<?php

class CategoryDetailController extends Controller
{
        public $layout = 'newlayout';
	public function actionIndex(){
           $ads = new CActiveDataProvider ('Ads'); // tao ra dataProvider moi
           $categories = Category::model()->findAll( ); // tao ra dataProvider moi
           $this->render ( 'index', array (
                    'ads' => $ads,
                    'categories' => $categories, 
		));
	}
        
        public function actionView($id)
	{
                $ads = new CActiveDataProvider ( 'Ads' ); // tao ra dataProvider moi
                $categories = Category::model()->findAll( ); // tao ra dataProvider moi
                $category = $this->loadModel($id);
                $login_form = new LoginForm;
                $this->render('view',array(
			'model'=> $category,
                        'ads' => $ads,
                        'categories' => $categories, 
                        'login_form' => $login_form,
		));
	}
        
        
        public function loadModel($id)
	{
		$model=Category::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
        
        
        public function actionScroll(){
		if(isset($_GET['id'])&&isset($_GET['nth'])){
		$news = Yii::app()->db->createCommand()
				->select('*')
				->from('tbtt_news')
				->where('category_id = '.$_GET['id'])
				->limit(1)
				->offset($_GET['nth'])
				->queryAll();
		if($news!=null){
		echo '<li>            
			<div class="news_title">
				<a href="index.php?r=detail/view&id='.$news[0]["news_id"].'">'.$news[0]["news_title"].'</a>
			</div>
			<div class="news_post_time">
				 '.$news[0]["news_created_at"].'
			</div>
			<div class="news_content">
				'.htmlspecialchars_decode($news[0]["news_description"]).'
			</div>
		</li>';}
		}
		//$this->render('scroll');
	}   
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}